package com.zhengxyou.jetpackdemo.dataBinding;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.zhengxyou.jetpackdemo.BR;
import com.zhengxyou.jetpackdemo.R;
import com.zhengxyou.jetpackdemo.databinding.ItemDataBindBinding;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

public class DataBindAdapter extends RecyclerView.Adapter<DataBindViewHolder> {
    private List<User> mData;

    public DataBindAdapter() {
        mData = new ArrayList<>();
    }

    @NonNull
    @Override
    public DataBindViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemDataBindBinding bindBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_data_bind, parent, false);
        DataBindViewHolder holder = new DataBindViewHolder(bindBinding.getRoot());
        holder.setBinding(bindBinding);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull DataBindViewHolder holder, int position) {
        holder.getBinding().setVariable(BR.user, mData.get(position));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(List<User> mData) {
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }
}
